/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package eac.ucm;
import common.ReConvertor;
import eac.view.*;
import eac.model.*;
import java.util.*;
import java.text.SimpleDateFormat;
import javax.swing.*;
import medium.MsgSender;

/**
 *
 * @author Vitamin
 */

public class AddValue {

    private ComponentModel componentA;
    private ValueModel valueA;
    private ComponentModel componentB;
    private ValueModel valueB;
    private ComponentModel componentC;
    private ValueModel valueC;
    private String course;
    private String mark;
    private String failed;
    private String teacher;
    private String student;
    private String id;
    private Date today;
    private String tep;

    private JTextArea response;
    private String sql;
    private static AddValue local;
    private String rst;

    public AddValue()
    {
        componentB=EACenter.getInstance().getComponentModel();
        valueB=EACenter.getInstance().getValueModel();
        id=valueB.getId();
        this.getToday();
    }

    public static AddValue getInstance(String tp)
    {
        if(local==null)
        {
            local=new AddValue();
            local.rst=tp;
        }
        return local;
    }

    public void getToday()
    {
        today=new Date();
        SimpleDateFormat kiper=new SimpleDateFormat("yyyy-MM-dd");
        tep=kiper.format(today);
    }

    public void addRecord()
    {
        componentA=AddRecordPanel.getInstance().getComponentModel();
        valueA=AddRecordPanel.getInstance().getValueModel();

        course=valueA.getAddCourse();
        failed=valueA.getAddFailed();
        mark=valueA.getAddMark();
        teacher=valueA.getAddTeacher();
        student=valueA.getAddStudent();

        if(failed.equalsIgnoreCase("YES"))
        {
            failed="Y";
        }
        else
        {
            failed="N";
        }

        String tp= id+";"+course+";"+teacher+";"+mark+";"+failed+";"+tep;
        MsgSender.getInstance().toAddNewAchievement(tp);
    }


    public void addCourse()
    {
        componentC=AddCoursePanel.getInstance().getComponentModel();
        valueC=AddCoursePanel.getInstance().getValueModel();

        sql="INSERT INTO course(course_id, teacher_id, subject, credit_hour, description, college) " +
                "VALUES('"+ valueC.getAddCourseID() +"', '"+ valueC.getAddTeacherID() +"','"+ valueC.getAddTitle() +"','"+ valueC.getAddCredit() +"','"+ valueC.getAddDesp() +"','"+ valueC.getAddCollege() +"')";
        String tp= valueC.getAddTeacherID()+";"+valueC.getAddCourseID()+";"+valueC.getAddTitle()+";"+valueC.getAddCredit()+";"+valueC.getAddDesp()+";"+ReConvertor.getInstance().college(valueC.getAddCollege());
        MsgSender.getInstance().toAddNewCourse(tp);
    }


    public void stResponse()
    {
        response=componentB.getResponse();
        if(rst.equalsIgnoreCase("Y"))
        {
        response.setText(response.getText()+"\n> 1 record commited successfully");
        }
        else
        {
            response.setText(response.getText()+"\n> Failed to commit");
        }
    }

    public void tResponse()
    {
        response=componentB.getTresponse();
        if(rst.equalsIgnoreCase("Y"))
        {
        response.setText(response.getText()+"\n> 1 record commited successfully");
        }
        else
        {
            response.setText(response.getText()+"\n> Failed to commit");
        }
    }


}
